package 数学问题;

public class 必备的求和公式 {
    public static void main(String[] args) {
        System.out.println(sum(2));
        System.out.println(sum2(3));
        System.out.println(sum3(3));
    }
    // 1 - n 的求和
    static int sum(int n){// 1 + 2 + 3 + 4 + ... + n
        int sum = 0;
        sum =  (1+n)*n/2;
        return n;
    }
    // 1  -  n 的平方和
    static int sum2(int n){// 1*1 + 2*2 +3*3 + 4*4 +...+ n*n
        int sum = 0;
        sum = n*(n+1)*(2*n+1)/6;
        return sum;
    }
    // 1  -  n 的立方和
    static int sum3(int n){
        int sum = 0;
        sum = n*n*(n+1)*(n+1)/4;
        return sum;
    }
}
